package demo04_route;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import utils.ConnectionUtil;
import utils.LogLevelEnum;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * @Description 路由模式的消息生产者
 * @Author shihan
 * @Date 2020/12/20 21:53
 * @Version 1.0
 */
public class RProducer {

    public static final String EXCHANGE_NAME_DIRECT_ROUTE = "exc.dir.route.ename";

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(ConnectionUtil.URI);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        // 声明 Exchange
        channel.exchangeDeclare(EXCHANGE_NAME_DIRECT_ROUTE, BuiltinExchangeType.DIRECT);
        // 发送消息
        channel.basicPublish(EXCHANGE_NAME_DIRECT_ROUTE, LogLevelEnum.INFO.getDesc(),null,"info 级别的日志！".getBytes("utf-8"));
        channel.basicPublish(EXCHANGE_NAME_DIRECT_ROUTE,LogLevelEnum.ERROR.getDesc(),null,"error 级别的日志！".getBytes("utf-8"));

        // 生产环境使用池化技术
        channel.close();
        connection.close();
    }

}
